﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WooCoo.ObjectModel
{
    public partial class EntityList<T> : ITrackStatus
    {
        #region ITrackStatus

        /// <summary>
        /// 对象是否发生过数据变更
        /// </summary>
        public bool IsDirty
        {
            get
            {
                // any non-new deletions make us dirty
                foreach (T item in DeletedList)
                    if (!item.IsNew)
                        return true;

                // run through all the child objects
                // and if any are dirty then then
                // collection is dirty
                foreach (T child in this)
                    if (child.IsDirty)
                        return true;
                return false;
            }
        }

        /// <summary>
        /// 对象是否可用
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (T child in this)
                    if (!child.IsValid)
                        return false;
                return true;
            }
        }

        public bool IsChild
        {
            get;
            private set;
        }

        void ITrackStatus.MarkClean()
        {
            this.MarkClean();
        }

        protected void MarkClean()
        {
            foreach (T child in this)
            {
                (child as ITrackStatus).MarkClean();
            }
        }

        #endregion

        protected void MarkAsChild()
        {
            this.IsChild = true;
        }
    }
}
